package com.dycong.common.common.thread.pool;

import java.util.concurrent.*;

/**
 * 线程池工具类
 * @author outh
 *
 */
public class ThreadPoolUtil {
	
	private static int corePoolSize = 30;
	private static int maximumPoolSize = 50;
	private static int keepAliveTime = 3;
	private static int queueSize = 150;
	private static ThreadPoolExecutor executor = new MyThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
			TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize), new NamedThreadFactory());
	
	private ThreadPoolUtil(){}
	
	public static ThreadPoolExecutor getTaskExecutor(){ 
		return executor; 
	}
	/**
	 * 添加一个任务到线程池
	 * @param runnable
	 */
	public static void  execute(Runnable runnable) {
		getTaskExecutor().execute(runnable);
	}
	
	public static <T> Future<T> submit(Callable<T> task) {
		return getTaskExecutor().submit(task);
	}
	/**
	 * 关闭线程池
	 */
	public static void shutdown() {
		if (executor != null) {
			executor.shutdown();
		}
	}

	public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
		Future future = ThreadPoolUtil.submit(()->{
			String[] strings= {"",""};
			return strings[2];
		});

		try {
			future.get(1,TimeUnit.SECONDS);
		}catch (Exception e){

		}


		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				String[] strings= {"",""};
				System.out.println(strings[3]);;
			}
		});
		thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread t, Throwable e) {
				System.out.println("UncaughtExceptionHandler catch" );
				e.printStackTrace();
			}
		});
		thread.start();
	}
}
